package components.game
{
	import SEApp.dialogs.se_alert;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_util;
	
	import classes.clt_events;
	
	import components.dialogs.clt_collageView;
	import components.tools.photos.clt_userAvatar;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;

	public class clt_commentItem extends clt_userAvatar {
		private var _comment: uif_label;
		public function clt_commentItem(index:int, parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, a_data: Object=null, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, a_data, styleClass);
		}
		
		override protected function createComponents():void {
			import UIFree.controls.uif_input;
			
			super.createComponents();
			var inner	: Rectangle = getInner();
			inner.left += _prevImage.width + 5;
			var isNew: Boolean = _dataItem.content_type == clt_comments.CONTENTTYPE_NEW;
			(new uif_label(this, inner.x, inner.y, inner.width, 16, 'timeCom')).text = _dataItem.time;
			inner.top += 12;
			_comment = new (isNew?uif_input:uif_label)(this, inner.x, inner.y, inner.width, inner.height, 'comment');
			if (isNew) {
				_comment.addEvent(KeyboardEvent.KEY_DOWN, doKey);
				hint = 'После ввода комментария нажмите "Enter" чтобы сохранить'; 
				stage.focus = _comment.textField;
			} else if (_dataItem.content_type == clt_comments.CONTENTTYPE_MSG) {
				_comment.hint = _dataItem.comment;
			} else _comment.text = _dataItem.comment
			
			_prevImage.addEvent(MouseEvent.CLICK, function(e: MouseEvent): void {
				navigateToURL(new URLRequest(_dataItem.link));
			});
		}
		
		public function doKey(e: KeyboardEvent): void {
			if (e.keyCode == 13) {
				if (_comment.text.match(/[\wА-я]/gi).length > 0) {
					_dataItem.comment = _comment.text;
					_comment.textField.type = 'dynamic';
					_comment.textField.selectable = false;
					list.dispatchEvent(new ObjectEvent(clt_comments.NEWCOMMENT, _dataItem));
				} else app.dispatchEvent(new ObjectEvent(se_alert.ALERT, 'Неверный формат строки'));	
			}
		}
		
		override protected function getMenuList():Array {
			var menuList: Array = [
				{caption: clt.classes.locale.USERCOLLAGES, method: 'doCollageView'}
			];
			
			if (_dataItem.uid != clt.externalServer.viewer_id) {
				menuList.push({caption: ' Перейти на страницу автора ', method: 'doBlog'});
				if (!clt.isBanUser)
					menuList.push({caption: ' Пожаловаться на комментарий ', method: 'doBanRequest'});
			} 
			if (isDelAccess) menuList.push({caption: ' Удалить ', method: 'doDelete'});
			return menuList;
		}
		
		protected function get isDelAccess(): Boolean {
			return ((_dataItem.uid == clt.externalServer.viewer_id) || 
					(collageData.uid == clt.externalServer.viewer_id)) && 
					(!_dataItem.content_type || (_dataItem.content_type == clt_comments.CONTENTTYPE_COM));
		}
		
		public function doBlog(): void {
			navigateToURL(new URLRequest(_dataItem.link));
		}
		
		public function doDelete(): void {
			clt.alert(clt.classes.locale.WARNING, clt.classes.locale.REQUESTDELETECOMMENT, function(): void {
				clt.internalServer.deleteComment(_dataItem.comment_id, function(r_data: Object): void {
					if (r_data.response[0].result) {
						(list.parent.parent.parent as clt_collageView).refreshComments();
					}
				});
			});
		}
		
		public function doCollageView(): void {
			clt.workspace.dispatchEvent(new ObjectEvent(clt_events.USERCOLLAGES, _dataItem));
		}
		
		public function doBanRequest(): void {
			clt.workspace.dispatchEvent(new ObjectEvent(clt_events.BANREQUEST, _dataItem));
		}
		
		protected function get collageData(): Object {
			return (list.parent.parent.parent as clt_collageView)._data;
		}
		
		override protected function buttonRelease(e:MouseEvent):void {
			menu.showMenu();
		} 
		
		override protected function sourceRect(): Rectangle {
			var inner: Rectangle = super.sourceRect();
			inner.width = inner.height;
			return inner;
		} 
	}
}